<template>
  <PageWrapper title="修改当前用户密码" content="修改成功后会自动退出当前登录！">
    <div class="py-8 bg-white flex flex-col justify-center items-center">
      <BasicForm @register="register" />
      <div class="flex justify-center">
        <a-button @click="resetFields"> 重置 </a-button>
        <a-button class="!ml-4" type="primary" @click="handleSubmit"> 确认 </a-button>
      </div>
    </div>
  </PageWrapper>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { router } from '/@/router';
  import { PageEnum } from '/@/enums/pageEnum';
  import { PageWrapper } from '/@/components/Page';
  import { BasicForm, useForm } from '/@/components/Form';
  import { useUserStore } from '/@/store/modules/user';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { ChangePwd } from '/@/api/demo/general';
  import { formSchema } from './pwd.data';

  export default defineComponent({
    name: 'ChangePassword',
    components: { BasicForm, PageWrapper },
    setup() {
      const userStore = useUserStore();
      const { createMessage } = useMessage();
      const [register, { validate, resetFields }] = useForm({
        size: 'large',
        labelWidth: 100,
        showActionButtonGroup: false,
        schemas: formSchema,
      });
      async function handleSubmit() {
        try {
          const values = await validate();
          const { passwordOld, passwordNew } = values;
          console.log(passwordOld, passwordNew);
          await ChangePwd({
            oldpassword: passwordOld,
            newpassword: passwordNew,
          });
          createMessage.success('密码修改成功', function () {
            //userStore.logout();
            router.push(PageEnum.BASE_LOGIN);
          });
          // createSuccessModal({
          //   title: 'Tip',
          //   content: '密码修改成功',
          //   onOk: function () {
          //     //调用api方法
          //     console.log('ok');
          //   },
          // });
          // // TODO custom api
          // await ChangePwd({
          //   oldpassword: passwordOld,
          //   newpassword: passwordNew,
          // });
          // //退出登陆
          // userStore.logout(true);
        } catch (error) {}
      }
      return { register, resetFields, handleSubmit };
    },
  });
</script>
<style lang="less" scoped>
  .justify-center {
    :deep(.ant-row) {
      flex-flow: inherit;
    }
  }
</style>
